﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InstantSpine.Core.ParaSpine
{
    public sealed class AnsysAbnormalExitException : Exception
    {
        public static AnsysExitConditions DecodeAnsysExitCode(int ansysReturnCode) {
            switch (ansysReturnCode) {
                case 0:  return AnsysExitConditions.NormalExit;
                case 1:
                case 2:
                case 3:
                case 4:  return AnsysExitConditions.StackError;
                case 5:  return AnsysExitConditions.CommandLineArgumentError;
                case 6:  return AnsysExitConditions.AccountingFileError;
                case 7:  return AnsysExitConditions.AuthFileVerificationError;
                case 8:  return AnsysExitConditions.EndOfRun;
                case 11: return AnsysExitConditions.UserRoutineError;
                case 12: return AnsysExitConditions.MacroSTOPCommand;
                case 14: return AnsysExitConditions.XOXError;
                case 15: return AnsysExitConditions.FatalError;
                case 16: return AnsysExitConditions.FullDisk;
                case 17: return AnsysExitConditions.CorruptedOrMissingFile;
                case 18: return AnsysExitConditions.CorruptedDBFile;
                case 21: return AnsysExitConditions.AuthorizedCodeSectionEntered;
                case 25: return AnsysExitConditions.UnableToOpenX11Server;
                case 30: return AnsysExitConditions.QuitSignal;
                case 31: return AnsysExitConditions.FailureToGetSignal;
                default:
                    if (ansysReturnCode >= 32) {
                        return AnsysExitConditions.SystemDependentError;
                    }
                    else
                        throw new InvalidOperationException(string.Format("Unknown ANSYS exit code {0}", ansysReturnCode));
            }
        }

        public int AnsysExitCode { get; private set; }

        public AnsysExitConditions AnsysExitCondition { get { return DecodeAnsysExitCode(AnsysExitCode); } }

        public AnsysAbnormalExitException(int ansysReturnCode) {
            this.AnsysExitCode = ansysReturnCode;
        }
    }
}
